package com.easyhouse.controller;


import com.easyhouse.common.pojo.Product;
import com.easyhouse.common.utils.MapperUtil;
import com.easyhouse.common.vo.SysResult;
import com.easyhouse.mapper.ProductMapper;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CreateIndex {
    @Autowired(required = false)
    private ProductMapper productMapper;
    @Autowired(required = false)
    private TransportClient client;

    //创建批量索引
    @RequestMapping("/index/create")
    public SysResult indexCreate() {
        //创建索引
        String indexName = "easyhouse";
        String type = "house";
        /** 1.判断是否存在,存在直接使用,不存在创建
         *  2.读取数据库数据start=0 rows=100
         *  3.将数据解析成json 放到索引
         */
        IndicesAdminClient indices = client.admin().indices();
        //存在
        IndicesExistsResponse existsResponse = indices.prepareExists(indexName).get();
        if (!existsResponse.isExists()) {
            indices.prepareCreate(indexName).get();
        }
        //读取SellStatus为0的数据
        List<Product> products = productMapper.selectProductBySellStatus();
        for (Product product : products) {
            try {
                String json = MapperUtil.MP.writeValueAsString(product);
                IndexRequestBuilder docRequest = client.prepareIndex(indexName, type, product.getHouseId());
                docRequest.setSource(json).get();
            } catch (Exception e) {
                e.printStackTrace();
                continue;
            }
        }
        return SysResult.ok();
    }

}
